package kr.hkit.powerjava.chapter17;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.NumberFormatter;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JFormattedTextField;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.Date;

public class TFormatted extends JFrame implements PropertyChangeListener {

	private JPanel contentPane;
	private JPanel panel;
	private JLabel lblNewLabel;
	private JFormattedTextField amountField;
	private JPanel panel_1;
	private JLabel lblNewLabel_1;
	private JFormattedTextField dateField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TFormatted frame = new TFormatted();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TFormatted() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		
		panel = new JPanel();
		contentPane.add(panel);
		
		lblNewLabel = new JLabel("금액");
		panel.add(lblNewLabel);
		
		amountField = new JFormattedTextField(new NumberFormatter());
		amountField.addPropertyChangeListener(this);
		amountField.setValue(new Integer(100));
		amountField.setColumns(10);
		
		panel.add(amountField);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1);
		
		lblNewLabel_1 = new JLabel("날짜");
		panel_1.add(lblNewLabel_1);
		
		dateField = new JFormattedTextField();
		dateField.addPropertyChangeListener(this);
		dateField.setValue(new Date());
		dateField.setColumns(10);
		
		panel_1.add(dateField);
	}

	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getSource() == dateField) {
			propertyChangeDateField(evt);
		}
		if (evt.getSource() == amountField) {
			propertyChangeAmountField(evt);
		}
	}
	protected void propertyChangeAmountField(PropertyChangeEvent evt) {
		Number amount = (Number) amountField.getValue();
		System.out.println(amount);
	}
	protected void propertyChangeDateField(PropertyChangeEvent evt) {
		Date d = (Date) dateField.getValue();
		System.out.println(d);
	}
}
